{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "70d8c20c",
   "metadata": {},
   "source": [
    "# Tensor advanced operation\n",
    "\n",
    "- Where\n",
    "\n",
    "- Gather"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "748c2656",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "40641896",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.8116, 0.3713],\n",
      "        [0.8448, 0.5871]])\n"
     ]
    }
   ],
   "source": [
    "cond = torch.rand(2, 2)\n",
    "print(cond)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "179e38e7",
   "metadata": {},
   "source": [
    "### Where"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "826f69ff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0., 0.],\n",
      "        [0., 0.]])\n",
      "tensor([[1., 1.],\n",
      "        [1., 1.]])\n",
      "tensor([[0., 1.],\n",
      "        [0., 0.]])\n"
     ]
    }
   ],
   "source": [
    "a = torch.zeros(2, 2)\n",
    "print(a)\n",
    "\n",
    "b = torch.ones(2, 2)\n",
    "print(b)\n",
    "\n",
    "# cond>0.5 的位置赋a的值，否则赋b的值 \n",
    "print(torch.where(cond > 0.5, a, b))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0039fdee",
   "metadata": {},
   "source": [
    "### Gather 收集（索引）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "796a9b7c",
   "metadata": {},
   "outputs": [],
   "source": [
    "GPU 索引替换取值"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
